package com.association.management.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.association.management.common.constant.AdminConstant;
import com.association.management.mbg.mapper.*;
import com.association.management.mbg.model.*;
import com.association.management.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by PanD
 */

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AssociationMapper associationMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private DutyGeneralSettingMapper dutyGeneralSettingMapper;

    @Resource
    private DutyTimeslotSettingMapper dutyTimeslotSettingMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private AssociationUserRelationshipMapper associationUserRelationshipMapper;

    @Override
    public Association addAssociation(Association association, Integer userId) {

        //生成code：取 社团名称 + 当前时间戳 进行md5，取前6位
        association.setCode(SecureUtil.md5(association.getName() + System.currentTimeMillis())
                .substring(0, 6)
                .toUpperCase());
        Integer associationId = associationMapper.insert(association);

        //插入创建人（超级管理元）信息
        Admin admin = new Admin();
        admin.setSubordinateAssociation(associationId);
        admin.setRoleValue(AdminConstant.SUPER_ADMIN);
        admin.setUserId(userId);

        //TODO 需不需要校验重复社团
        return associationMapper.selectByPrimaryKey(associationId);
    }

    @Override
    public int deleteAssociation(Integer associationId) {
        //TODO 删除该部门的所有操作
        //删除所有相关数据
        dutyGeneralSettingMapper.deleteBySubordinateAssociation(associationId);
        dutyTimeslotSettingMapper.deleteBySubordinateAssociation(associationId);
        departmentMapper.deleteBySubordinateAssociation(associationId);
        roleMapper.deleteBySubordinateAssociation(associationId);
        associationUserRelationshipMapper.deleteBySubordinateAssociation(associationId);
        adminMapper.deleteBySubordinateAssociation(associationId);
        return associationMapper.deleteByPrimaryKey(associationId);
    }

    @Override
    public Association updateAssociation(Association association) {
        Integer associationId = associationMapper.updateByPrimaryKeySelective(association);
        return associationMapper.selectByPrimaryKey(associationId);
    }

    @Override
    public List<Association> getAllAssociation(User user) {
        //TODO 是否合并到普通操作业务
//        List<Association> associationList = adminMapper.selectByExample();
        return null;
    }

// 部门相关 ---------------------------------------------------------------------------------------


    @Override
    public Department addDepartment(Department department, Integer associaionId) {
        department.setSubordinateAssociation(associaionId);
        departmentMapper.insert(department);
        return departmentMapper.selectByPrimaryKey(associaionId);
    }

    @Override
    public Department updateDepartment(Department department, Integer associationId) {
        return null;
    }

    @Override
    public int deleteDepartment(Integer departmentId, Integer associationId) {
        return 0;
    }

    @Override
    public List<Department> getAllDepartmentByAssociationId(Integer associationId) {
        return null;
    }


// 管理员相关 --------------------------------------------------------------------------------------

    @Override
    public Admin addAdmin(Admin admin, Integer associationId) {
        admin.setSubordinateAssociation(associationId);
        return adminMapper.selectByPrimaryKey(adminMapper.insert(admin));
    }

    @Override
    public int deleteAdmin(Integer adminId) {
        return adminMapper.deleteByPrimaryKey(adminId);
    }

    @Override
    public List<Admin> getAllAdmin(Admin admin) {
        //TODO 查看当前部门所有管理员
        return null;
    }
}
